package com.sudichina.sudichina.view.activity.setting;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.common.reflect.TypeToken;
import com.sudichina.sudichina.MyApp;
import com.sudichina.sudichina.NET.Api;
import com.sudichina.sudichina.NET.NetUtils;
import com.sudichina.sudichina.NET.onRequestDataFinish;
import com.sudichina.sudichina.R;
import com.sudichina.sudichina.bean.Result;
import com.sudichina.sudichina.utils.DESMD5Utils;
import com.sudichina.sudichina.utils.Toastutils;
import com.sudichina.sudichina.utils.Util;
import com.sudichina.sudichina.view.activity.main.BaseActivity;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class SetDealPswActivity extends BaseActivity implements onRequestDataFinish {

    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_right)
    TextView tvRight;
    @BindView(R.id.et_asdp_setdealpsw)
    EditText etAsdpSetdealpsw;
    @BindView(R.id.bt_asdp_setdealpsw)
    Button btAsdpSetdealpsw;
    @BindView(R.id.activity_set_deal_psw)
    LinearLayout activitySetDealPsw;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_set_deal_psw);
        ButterKnife.bind(this);
        tvTitle.setText("设置交易密码");
    }

    @OnClick({R.id.iv_back, R.id.bt_asdp_setdealpsw})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.bt_asdp_setdealpsw:
                boolean IsPassword = Util.IsPassword(etAsdpSetdealpsw.getText().toString());
                if (!IsPassword) {
                    Toastutils.showToast("密码由6-16数字和字母组成");
                    return;
                }
                String md5_pwd = DESMD5Utils.MD5(etAsdpSetdealpsw.getText().toString(), false);
                submitPwd(md5_pwd);


                break;
        }
    }

    private void submitPwd(String md5_pwd) {

        HashMap<String, Object> map = new HashMap<>();
        /*accountName: String 手机号
accountPassword：String 密码  MD5加密*/
        map.put("accountName", MyApp.getCurrentUser().getAccountName().toString());
        map.put("tradePassword", md5_pwd);
        Type type = new TypeToken<Result>() {
        }.getType();
        NetUtils.requestData(Api.FORGOT, map, this, type, false);
    }

    @Override
    public void requestdataFinish(String catalog, Object bean) {
        Result bean1 = (Result) bean;
        int code = bean1.getCode();
        switch (code) {
            case 8016:
                Toastutils.showToast("密码已经存在 无须设置");
                finish();
                break;
            case 1:
                Toastutils.showToast("设置成功");
                MyApp.getCurrentUser().setTradePassword(DESMD5Utils.MD5(etAsdpSetdealpsw.getText().toString(), false));
                finish();

                break;
        }

    }

    @Override
    public void requestdataFailed(IOException e) {
        Toastutils.showToast("IOException:" + e);

    }
}
